package com.example.llm.sgg.chat.config;

import com.example.llm.sgg.chat.handler.ChatWebSocketMessageHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;
import org.springframework.web.reactive.socket.WebSocketHandler;
import org.springframework.web.reactive.socket.server.support.WebSocketHandlerAdapter;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RouterConfig {

    // 注册自定义 Handler
    @Bean
    public SimpleUrlHandlerMapping handlerMapping(ChatWebSocketMessageHandler webSocketHandler) {
        Map<String, WebSocketHandler> map = new HashMap<>();
        map.put("/example/chat", webSocketHandler); // 绑定到 /example/chat 路径
        SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
        mapping.setUrlMap(map);
        mapping.setOrder(1); // 设置优先级
        return mapping;
    }

    // 必需：启用 WebSocket 支持
    @Bean
    public WebSocketHandlerAdapter handlerAdapter() {
        return new WebSocketHandlerAdapter();
    }

}